#! /usr/bin/env python
#coding=utf-8

"""
    sqlalchemy的测试2
"""

from sqlalchemy import *
from sqlalchemy.orm import *

engine = create_engine("sqlite:///tutorial.db", echo = True)
metadata = MetaData()

users_table=Table('user',metadata,
    Column('user_id',Integer,primary_key=True),
    Column('user_name',String(40)),
    Column('password',String(10))
)

email_addresses_table=Table('email_addresses',metadata,
    Column('address_id',Integer,primary_key=True),
    Column('email_address',String(100),nullable=False),
    Column('user_id',Integer,ForeignKey('user.user_id'))
)


#metadata.drop_all(engine)
#metadata.create_all(engine)

class User(object):
    def __init__(self, *args, **kwargs):
        if len(args) == 2:
            self.user_name = args[0]
            self.password = args[1]
    
    def __repr__(self):
        return "%s(%r,%r)"%(
            self.__class__.__name__,self.user_name,self.password)

usermapper = mapper(User,users_table)

session = create_session(bind=engine)
query = session.query(User)
print query.filter_by(user_name='Harry').one()

ed = User('Ed', 'edspassword')
session.save(ed)
session.flush()






